package net.alche.fa.core.doing;

import net.alche.fa.core.model.FaValue;
import java.util.List;
import net.alche.fa.core.CodeLevel;
import net.alche.fa.core.FaException;
import java.util.ArrayList;
import net.alche.al.util.AC;
import java.util.Date;

public class FaLogUtil {

  public FaValue log(List<FaValue> list, CodeLevel lvl) throws FaException {
    String log = "";
    List<Object> javaList = new ArrayList<> ();
    int count = 0;
    for ( FaValue ob: list) {
      if ( ob==null) {
        continue;
      }
      javaList.add(ob.getJava());
      log += (count > 1 ? "," : "") + (ob != null && ob.isNull() ? "null" : getObStr(ob));
      count++;
    }
    String logTxt = AC.date.fmt(new Date(), AC.date.FMT_TIME) + " [log] " + log;
    System.out.println(logTxt);
    // if ( lvl.sageBiz != null) {
    //   lvl.sageBiz.sendDebugMsg(lvl, "fa-log", logTxt);
    // }
    return null;
  }

  public static void logException(String errorInfo, CodeLevel lvl) {
    String logTxt = AC.date.fmt(new Date(), AC.date.FMT_TIME) + " [error] " + errorInfo;
    System.out.println(logTxt);
    // if ( lvl != null && lvl.sageBiz != null) {
    //   lvl.sageBiz.sendDebugMsg(lvl, "fa-log", logTxt);
    // }
  }

  public String getObStr(FaValue ob) {
    String obStr = "";
    if ( ob == null) {
      return "null";
    }
    if ( ob.getType() == FaValue._DATE) {
      obStr = AC.date.fmtDatetime(ob.getDateValue());
    }
    else if ( ob.getType() == FaValue._MAP || ob.getType() == FaValue._LIST) {
      obStr = AC.json.toStr(ob.getJava());
    }
    else if ( ob.getType() == FaValue._SAGE) {
      obStr = "Sage[" + ob.getObjType() + "]" + AC.json.toStr(ob.getJava());
    }
    else {
      obStr = ob.getJava() + "";
    }
    return obStr;
  }
}